package com.taxi.driver.module.amap.navi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.AMapNaviView;
import com.amap.api.navi.model.NaviLatLng;
import com.qianxx.utils.RxUtil;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.util.SpeechUtil;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;

/**
 * 独立的导航页
 */
public class SingleRouteCalculateActivity extends BaseNaviActivity {

    private Subscription mSubscribe;
    private boolean isNaviCancel = false;

    public static void actionStart(Context context, LatLng originLatLng, LatLng destLatLng) {
        Intent intent = new Intent(context, SingleRouteCalculateActivity.class);
        intent.putExtra(IConstants.ORIGIN, originLatLng);
        intent.putExtra(IConstants.DEST, destLatLng);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getExtra(); //处理传递过来的数据

        setContentView(R.layout.activity_navigate);
        mAMapNaviView = (AMapNaviView) findViewById(R.id.navi_view);
        mAMapNaviView.onCreate(savedInstanceState);
        mAMapNaviView.setAMapNaviViewListener(this);

        SpeechUtil.speech(this, "导航开始");
    }

    private void getExtra() {
        LatLng orgin = getIntent().getParcelableExtra(IConstants.ORIGIN);
        LatLng dest = getIntent().getParcelableExtra(IConstants.DEST);
        mStartLatlng = new NaviLatLng(orgin.latitude, orgin.longitude);
        mEndLatlng = new NaviLatLng(dest.latitude, dest.longitude);
        sList.add(mStartLatlng);
        eList.add(mEndLatlng);
    }

    @Override
    public void onInitNaviSuccess() {
        super.onInitNaviSuccess();
        /**
         * 方法: int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute); 参数:
         *
         * @congestion 躲避拥堵
         * @avoidhightspeed 不走高速
         * @cost 避免收费
         * @hightspeed 高速优先
         * @multipleroute 多路径
         *
         *  说明: 以上参数都是boolean类型，其中multipleroute参数表示是否多条路线，如果为true则此策略会算出多条路线。
         *  注意: 不走高速与高速优先不能同时为true 高速优先与避免收费不能同时为true
         */
        int strategy = 0;
        try {
            //再次强调，最后一个参数为true时代表多路径，否则代表单路径
            strategy = mAMapNavi.strategyConvert(true, false, false, false, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy);
    }




    @Override
    public boolean onNaviBackClick() {
        isNaviCancel = true;
        finish();
        return true;
    }

    @Override
    public void onBackPressed() {
        isNaviCancel = true;
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isNaviCancel) {
            SpeechUtil.speech(this.getApplicationContext(), "导航结束");
        }
    }

    @Override
    public void onNaviMapMode(int isLock) {
        if (isLock == 1) {
            if (mSubscribe != null && !mSubscribe.isUnsubscribed()) {
                mSubscribe.unsubscribe();
            }
            mSubscribe = Observable.timer(5, TimeUnit.SECONDS)
                    .compose(RxUtil.applySchedulers())
                    .subscribe(aLong -> mAMapNaviView.recoverLockMode(),
                            KLog::e);
        }
    }
}
